package com.hjy.backend.dataManager.page;

import com.hjy.backend.dataManager.pageCache.PageCache;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 数据页
 */
public class PageImpl implements Page {

    private int pageNumber; // 页面的页号
    private byte[] data; // 存储的数据
    private boolean dirty; // 是否是脏页面, 脏页面指的是内存中更新但是还没刷回磁盘的数据页
    private Lock lock;

    private PageCache pageCache;


    public PageImpl(int pageNumber, byte[] data, PageCache pc) {
        this.pageNumber = pageNumber;
        this.data = data;
        this.pageCache = pc;
        lock = new ReentrantLock();
    }

    @Override
    public void lock() {
        lock.lock();
    }

    @Override
    public void unlock() {
        lock.unlock();
    }

    /**
     * 释放当前page
     */
    @Override
    public void release() {
        pageCache.release(this);
    }

    @Override
    public void setDirty(boolean dirty) {
        this.dirty = dirty;
    }

    @Override
    public boolean isDirty() {
        return dirty;
    }

    @Override
    public int getPageNumber() {
        return pageNumber;
    }

    @Override
    public byte[] getData() {
        return data;
    }
}
